home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Utilities / Type1Manager / src / spaces.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-12  |  26.4 KB  |  949 lines

  1. /* $XConsortium: spaces.c,v 1.7 94/02/06 16:26:52 gildea Exp $ */
  2. /* Copyright International Business Machines, Corp. 1991
  3.  * All Rights Reserved
  4.  * Copyright Lexmark International, Inc. 1991
  5.  * All Rights Reserved
  6.  *
  7.  * License to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both that copyright notice and this permission notice appear in
  11.  * supporting documentation, and that the name of IBM or Lexmark not be
  12.  * used in advertising or publicity pertaining to distribution of the
  13.  * software without specific, written prior permission.
  14.  *
  15.  * IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF
  16.  * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  18.  * AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  THE ENTIRE RISK AS TO THE
  19.  * QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
  20.  * OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY PORTION OF THE
  21.  * SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE
  22.  * ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION.  IN NO EVENT SHALL
  23.  * IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  24.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  25.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  26.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  27.  * THIS SOFTWARE.
  28.  */
  29.  
  30. /*
  31.  * SPACES Module - Handles Coordinate Spaces
  32.  *
  33.  * This module is responsible for handling the TYPE1IMAGER "XYspace" object.
  34.  *
  35.  * &author. Jeffrey B. Lotspiech (lotspiech@almaden.ibm.com)
  36.  *
  37.  *
  38.  * Include Files
  39.  */
  40.  
  41. #ifndef T1GST
  42. #include "global.h"
  43. #endif
  44.  
  45. //static int FindDeviceContext(pointer device);
  46. static void ConsiderContext(struct xobject *obj, double M[2][2]);
  47. static void FillOutFcns(struct XYspace *S);
  48. static void FindFfcn(double cx, double cy, fractpel(**fcnP) ());
  49. static void FindIfcn(double cx, double cy, fractpel *icxP, fractpel *icyP, fractpel(**fcnP) ());
  50.  
  51. /*
  52.  * Other modules can call the SPACES module through function
  53.  * vectors in the "XYspace" structure.  The entry points accessed that
  54.  * way are "FConvert()", "IConvert()", and "ForceFloat()".
  55.  */
  56.  
  57.  
  58. #define    RESERVED  10        /* 'n' IDs are reserved for invalid & immortal spaces */
  59. #define    NEXTID    ((SpaceID < RESERVED) ? (SpaceID = RESERVED) : ++SpaceID)
  60.  
  61.  
  62. static unsigned int SpaceID = 1;
  63.  
  64.  
  65. struct XYspace *t1_CopySpace(struct XYspace *S)
  66. {
  67.     S = (struct XYspace *)Allocate(sizeof(struct XYspace), S, 0);
  68.  
  69.     S->ID = NEXTID;
  70.     return (S);
  71. }
  72.  
  73.  
  74. /*
  75.  * Lazy Evaluation of Matrix Inverses
  76.  *
  77.  * Calculating the inverse of a matrix is somewhat involved, and we usually
  78.  * do not need them.  So, we flag whether or not the space has the inverse
  79.  * already calculated:
  80.  */
  81. #define    HASINVERSE(flag)   ((flag)&0x80)
  82.  
  83.  
  84. /*
  85.  * The following macro forces a space to have an inverse:
  86.  */
  87. #define    CoerceInverse(S)   if (!HASINVERSE((S)->flag)) { \
  88.     MatrixInvert((S)->tofract.normal, (S)->tofract.inverse); (S)->flag |= HASINVERSE(ON); }
  89.  
  90.  
  91. /*
  92.  * IDENTITY Space
  93.  *
  94.  * IDENTITY space is (logically) the space corresponding to the identity
  95.  * transformation matrix.  However, since all our transformation matrices
  96.  * have a common FRACTFLOAT scale factor to convert to 'fractpel's, that
  97.  * is actually what we store in 'tofract' matrix of IDENTITY:
  98.  */
  99. static struct XYspace identity =
  100. {
  101.     SPACETYPE,
  102.     ISPERMANENT(ON) + ISIMMORTAL(ON) + HASINVERSE(ON), 2, /* added 3-26-91 PNM */
  103.     NULL, NULL,
  104.     NULL, NULL, NULL, NULL,
  105.     INVALIDID + 1, 0,
  106.     FRACTFLOAT, 0.0, 0.0, FRACTFLOAT,
  107.     1.0 / FRACTFLOAT, 0.0, 0.0, 1.0 / FRACTFLOAT,
  108.     0, 0, 0, 0
  109. };
  110. struct XYspace *IDENTITY = &identity;
  111.  
  112.  
  113. #define  MAXCONTEXTS   16
  114.  
  115. static struct doublematrix contexts[MAXCONTEXTS];
  116.  
  117. #ifdef notdef
  118.  
  119. static int nextcontext = 1;
  120.  
  121. #define   pointer          void *
  122.  
  123.  
  124. /*
  125.  * FindDeviceContext() - Find the Context Given a Device
  126.  *
  127.  * This routine, given a device, returns the index of the device's
  128.  * transformation matrix in the context array.  If it cannot find it,
  129.  * it will allocate a new array entry and fill it out.
  130.  */
  131. static int FindDeviceContext(pointer device)
  132. {
  133.     double M[2][2];        /* temporary matrix                             */
  134.     double Xres, Yres;    /* device  resolution                           */
  135.     int orient = -1;    /* device orientation                           */
  136.     int rc = -1;        /* return code for QueryDeviceState             */
  137.  
  138.     if (rc != 0)        /* we only bother with this check once          */
  139.         t1_abort("Context:  QueryDeviceState didn't work");
  140.  
  141.     M[0][0] = M[1][0] = M[0][1] = M[1][1] = 0.0;
  142.  
  143.     switch (orient)
  144.     {
  145.     case 0:
  146.         M[0][0] = Xres;
  147.         M[1][1] = -Yres;
  148.         break;
  149.     case 1:
  150.         M[1][0] = Yres;
  151.         M[0][1] = Xres;
  152.         break;
  153.     case 2:
  154.         M[0][0] = -Xres;
  155.         M[1][1] = Yres;
  156.         break;
  157.     case 3:
  158.         M[1][0] = -Yres;
  159.         M[0][1] = -Xres;
  160.         break;
  161.     default:
  162.         t1_abort("QueryDeviceState returned invalid orientation");
  163.     }
  164.     return (FindContext(M));
  165. }
  166.  
  167. /*
  168. :h3.FindContext() - Find the Context Given a Matrix
  169.  
  170. This routine, given a matrix, returns the index of that matrix matrix in
  171. the context array.  If it cannot find it, it will allocate a new array
  172. entry and fill it out.
  173. */
  174.  
  175. int FindContext(double M[2][2])
  176. {
  177.     register int i;        /* loop variable for search                     */
  178.     for (i = 0; i < nextcontext; i++)
  179.         if (M[0][0] == contexts[i].normal[0][0] && M[1][0] == contexts[i].normal[1][0]
  180.             && M[0][1] == contexts[i].normal[0][1] && M[1][1] == contexts[i].normal[1][1])
  181.             break;
  182.  
  183.     if (i >= nextcontext)
  184.     {
  185.         if (i >= MAXCONTEXTS)
  186.             t1_abort("Context:  out of them");
  187.         LONGCOPY(contexts[i].normal, M, sizeof(contexts[i].normal));
  188.         MatrixInvert(M, contexts[i].inverse);
  189.         nextcontext++;
  190.     }
  191.  
  192.     return (i);
  193. }
  194.  
  195. /*
  196. :h3.Context() - Create a Coordinate Space for a Device
  197.  
  198. This user operator is implemented by first finding the device context
  199. array index, then transforming IDENTITY space to create an appropriate
  200. coordinate space.
  201. */
  202.  
  203. struct XYspace *Context(
  204.         pointer device,                /* device token */
  205.         double units)                /* multiples of one inch */
  206. {
  207.     double M[2][2];        /* device transformation matrix                 */
  208.     int n;        /* will hold device context number              */
  209.     struct XYspace *S;    /* XYspace constructed                   */
  210.  
  211.     IfTrace2((MustTraceCalls), "Context(%x, %f)\n", device, &units);
  212.  
  213.     ARGCHECK((device == NULL), "Context of NULLDEVICE not allowed",
  214.          NULL, IDENTITY, (0), struct XYspace *);
  215.     ARGCHECK((units == 0.0), "Context: bad units", NULL, IDENTITY, (0), struct XYspace *);
  216.  
  217.     n = FindDeviceContext(device);
  218.  
  219.     LONGCOPY(M, contexts[n].normal, sizeof(M));
  220.  
  221.     M[0][0] *= units;
  222.     M[0][1] *= units;
  223.     M[1][0] *= units;
  224.     M[1][1] *= units;
  225.  
  226.     S = (struct XYspace *)Xform(IDENTITY, M);
  227.  
  228.     S->context = n;
  229.     return (S);
  230. }
  231. #endif
  232.  
  233.  
  234. /*
  235.  * ConsiderContext() - Adjust a Matrix to Take Out Device Transform
  236.  *
  237.  * Remember, we have (x times U times D) and M and and we want (x
  238.  * times U times M times D).  An easy way to do this is to calculate
  239.  * (D sup <-1> times M times D), because:
  240.  * (x times U times D times D sup <-1> times M times D) = (x times U times M times D)
  241.  *
  242.  * So this subroutine, given an (M) and an object, finds the (D) for that
  243.  * object and modifies (M) so it is (D sup <-1> times M times D).
  244.  */
  245. static void ConsiderContext(struct xobject *obj, double M[2][2])
  246. {
  247.     int context;    /* index in contexts array */
  248.  
  249.     if (obj == NULL)
  250.         return;
  251.  
  252.     if (ISPATHTYPE(obj->type))
  253.     {
  254.         struct segment *path = (struct segment *)obj;
  255.  
  256.         context = path->context;
  257.     }
  258.     else if (obj->type == SPACETYPE)
  259.     {
  260.         struct XYspace *S = (struct XYspace *)obj;
  261.  
  262.         context = S->context;
  263.     }
  264.     else if (obj->type == PICTURETYPE)
  265.     {
  266.  
  267.     }
  268.     else
  269.         context = NULLCONTEXT;
  270.  
  271.     if (context != NULLCONTEXT)
  272.     {
  273.         MatrixMultiply(contexts[context].inverse, M, M);
  274.         MatrixMultiply(M, contexts[context].normal, M);
  275.     }
  276. }
  277.  
  278.  
  279. /*
  280.  * Conversion from User's X,Y to "fractpel" X,Y
  281.  *
  282.  * When the user is building paths (lines, moves, curves, etc.) he passes
  283.  * the control points (x,y) for the paths together with an XYspace.  We
  284.  * must convert from the user's (x,y) to our internal representation
  285.  * which is in pels (fractpels, actually).  This involves transforming
  286.  * the user's (x,y) under the coordinate space transformation.  It is
  287.  * important that we do this quickly.  So, we store pointers to different
  288.  * conversion functions right in the XYspace structure.  This allows us
  289.  * to have simpler special case functions for the more commonly
  290.  * encountered types of transformations.
  291.  *
  292.  * Convert(), IConvert(), and ForceFloat() - Called Through "XYspace" Structure
  293.  *
  294.  * These are functions that fit in the "convert" and "iconvert" function
  295.  * pointers in the XYspace structure.  They call the "xconvert", "yconvert",
  296.  * "ixconvert", and "iyconvert" as appropriate to actually do the work.
  297.  * These secondary routines come in many flavors to handle different
  298.  * special cases as quickly as possible.
  299.  */
  300. static void __stdargs FXYConvert(
  301.         struct fractpoint *pt,        /* point to set */
  302.         struct XYspace *S,        /* relevant coordinate space */
  303.         double x, double y)        /* user's coordinates of point */
  304. {
  305.     pt->x = (*S->xconvert) (S->tofract.normal[0][0], S->tofract.normal[1][0], x, y);
  306.     pt->y = (*S->yconvert) (S->tofract.normal[0][1], S->tofract.normal[1][1], x, y);
  307.     return;
  308. }
  309.  
  310.  
  311. static void __stdargs IXYConvert(
  312.         struct fractpoint *pt,        /* point to set */
  313.         struct XYspace *S,        /* relevant coordinate space */
  314.         long x, long y)            /* user's coordinates of point */
  315. {
  316.     pt->x = (*S->ixconvert) (S->itofract[0][0], S->itofract[1][0], x, y);
  317.     pt->y = (*S->iyconvert) (S->itofract[0][1], S->itofract[1][1], x, y);
  318.     return;
  319. }
  320.  
  321.  
  322. /*
  323.  * ForceFloat is a substitute for IConvert(), when we just do not have
  324.  * enough significant digits in the coefficients to get high enough
  325.  * precision in the answer with fixed point arithmetic.  So, we force the
  326.  * integers to floats, and do the arithmetic all with floats:
  327.  */
  328.  
  329. static void __stdargs ForceFloat(
  330.         struct fractpoint *pt,        /* point to set */
  331.         struct XYspace *S,         /* relevant coordinate space */
  332.         long x, long y)            /* user's coordinates of point */
  333. {
  334.     (*S->convert) (pt, S, (double)x, (double)y);
  335.     return;
  336. }
  337.  
  338.  
  339. /*
  340.  * FXYboth(), FXonly(), FYonly() - Floating Point Conversion
  341.  *
  342.  * These are the routines we use when the user has given us floating
  343.  * point numbers for x and y. FXYboth() is the general purpose routine;
  344.  * FXonly() and FYonly() are special cases when one of the coefficients
  345.  * is 0.0.
  346.  */
  347. static fractpel __stdargs FXYboth(
  348.         double cx, double cy,        /* x and y coefficients */
  349.         double x, double y)        /* user x,y */
  350. {
  351.     double r;
  352.  
  353.     r = x * cx + y * cy;
  354.     return ((fractpel) r);
  355. }
  356.  
  357.  
  358. static fractpel __stdargs FXonly(
  359.         double cx, double cy,        /* x and y coefficients */
  360.         double x, double y)        /* user x,y */
  361. {
  362.     double r;
  363.  
  364.     r = x * cx;
  365.     return ((fractpel) r);
  366. }
  367.  
  368.  
  369. static fractpel __stdargs FYonly(
  370.         double cx, double cy,        /* x and y coefficients */
  371.         double x, double y)        /* user x,y */
  372. {
  373.     double r;
  374.  
  375.     r = y * cy;
  376.     return ((fractpel) r);
  377. }
  378.  
  379.  
  380. /*
  381.  * IXYboth(), IXonly(), IYonly() - Simple Integer Conversion
  382.  *
  383.  * These are the routines we use when the user has given us integers for
  384.  * x and y, and the coefficients have enough significant digits to
  385.  * provide precise answers with only "long" (32 bit?) multiplication.
  386.  * IXYboth() is the general purpose routine; IXonly() and IYonly() are
  387.  * special cases when one of the coefficients is 0.
  388.  */
  389. static fractpel __stdargs IXYboth(
  390.         fractpel cx, fractpel cy,    /* x and y coefficients */
  391.         long x, long y)            /* user x,y */
  392. {
  393.     return (x * cx + y * cy);
  394. }
  395.  
  396.  
  397. static fractpel __stdargs IXonly(
  398.         fractpel cx, fractpel cy,    /* x and y coefficients */
  399.         long x, long y)            /* user x,y */
  400. {
  401.     return (x * cx);
  402. }
  403.  
  404.  
  405. static fractpel __stdargs IYonly(
  406.         fractpel cx, fractpel cy,    /* x and y coefficients */
  407.         long x, long y)            /* user x,y */
  408. {
  409.     return (y * cy);
  410. }
  411.  
  412.  
  413. /*
  414.  * FPXYboth(), FPXonly(), FPYonly() - More Involved Integer Conversion
  415.  *
  416.  * These are the routines we use when the user has given us integers for
  417.  * x and y, but the coefficients do not have enough significant digits to
  418.  * provide precise answers with only "long" (32 bit?)  multiplication.
  419.  * We have increased the number of significant bits in the coefficients
  420.  * by FRACTBITS; therefore we must use "double long" (64 bit?)
  421.  * multiplication by calling FPmult().  FPXYboth() is the general purpose
  422.  * routine; FPXonly() and FPYonly() are special cases when one of the
  423.  * coefficients is 0.
  424.  *
  425.  * Note that it is perfectly possible for us to calculate X with the
  426.  * "FP" method and Y with the "I" method, or vice versa.  It all depends
  427.  * on how the functions in the XYspace structure are filled out.
  428.  */
  429. static fractpel __stdargs FPXYboth(
  430.         fractpel cx, fractpel cy,    /* x and y coefficients */
  431.         long x, long y)                /* user x,y */
  432. {
  433.     return (FPmult(x, cx) + FPmult(y, cy));
  434. }
  435.  
  436.  
  437. static fractpel __stdargs FPXonly(
  438.         fractpel cx, fractpel cy,    /* x and y coefficients */
  439.         long x, long y)                /* user x,y */
  440. {
  441.     return (FPmult(x, cx));
  442. }
  443.  
  444.  
  445. static fractpel __stdargs FPYonly(
  446.         fractpel cx, fractpel cy,    /* x and y coefficients */
  447.         long x, long y)                /* user x,y */
  448. {
  449.     return (FPmult(y, cy));
  450. }
  451.  
  452.  
  453. /*
  454.  * FillOutFcns() - Determine the Appropriate Functions to Use for Conversion
  455.  *
  456.  * This function fills out the "convert" and "iconvert" function pointers
  457.  * in an XYspace structure, and also fills the "helper"
  458.  * functions that actually do the work.
  459.  */
  460. static void FillOutFcns(struct XYspace *S)
  461. {
  462.     S->convert = FXYConvert;
  463.     S->iconvert = IXYConvert;
  464.  
  465.     FindFfcn(S->tofract.normal[0][0], S->tofract.normal[1][0], &S->xconvert);
  466.     FindFfcn(S->tofract.normal[0][1], S->tofract.normal[1][1], &S->yconvert);
  467.     FindIfcn(S->tofract.normal[0][0], S->tofract.normal[1][0],
  468.          &S->itofract[0][0], &S->itofract[1][0], &S->ixconvert);
  469.     FindIfcn(S->tofract.normal[0][1], S->tofract.normal[1][1],
  470.          &S->itofract[0][1], &S->itofract[1][1], &S->iyconvert);
  471.  
  472.     if (S->ixconvert == NULL || S->iyconvert == NULL)
  473.         S->iconvert = ForceFloat;
  474. }
  475.  
  476.  
  477. /*
  478.  * FindFfcn() - Subroutine of FillOutFcns() to Fill Out Floating Functions
  479.  *
  480.  * This function tests for the special case of one of the coefficients
  481.  * being zero:
  482.  */
  483. static void FindFfcn(
  484.         double cx, double cy,    /* x and y coefficients */
  485.         fractpel(**fcnP) ())    /* pointer to function to set */
  486. {
  487.     if (cx == 0.0)
  488.         *fcnP = FYonly;
  489.     else if (cy == 0.0)
  490.         *fcnP = FXonly;
  491.     else
  492.         *fcnP = FXYboth;
  493. }
  494.  
  495.  
  496. /*
  497.  * FindIfcn() - Subroutine of FillOutFcns() to Fill Out Integer Functions
  498.  *
  499.  * There are two types of integer functions, the 'I' type and the 'FP' type.
  500.  * We use the I type functions when we are satisfied with simple integer
  501.  * arithmetic.  We used the FP functions when we feel we need higher
  502.  * precision (but still fixed point) arithmetic.  If all else fails,
  503.  * we store a NULL indicating that this we should do the conversion in
  504.  * floating point.
  505.  */
  506. static void FindIfcn(
  507.         double cx, double cy,            /* x and y coefficients */
  508.         fractpel *icxP, fractpel *icyP,    /* fixed point coefficients to set */
  509.         fractpel(**fcnP) ())            /* pointer to function to set */
  510. {
  511.     fractpel imax;    /* maximum of cx and cy                      */
  512.  
  513.     *icxP = cx;
  514.     *icyP = cy;
  515.  
  516.     if (cx != (double)(*icxP) || cy != (double)(*icyP))
  517.     {
  518. /*
  519. At this point we know our integer approximations of the coefficients
  520. are not exact.  However, we will still use them if the maximum
  521. coefficient will not fit in a 'fractpel'.   Of course, we have little
  522. choice at that point, but we haven't lost that much precision by
  523. staying with integer arithmetic.  We have enough significant digits
  524. so that
  525. any error we introduce is less than one part in 2^16.
  526. */
  527.  
  528.         imax = MAX(ABS(*icxP), ABS(*icyP));
  529.         if (imax < (fractpel) (1 << (FRACTBITS - 1)))
  530.         {
  531. /*
  532. At this point we know our integer approximations just do not have
  533. enough significant digits for accuracy.  We will add FRACTBITS
  534. significant digits to the coefficients (by multiplying them by
  535. 1<<FRACTBITS) and go to the "FP" form of the functions.  First, we
  536. check to see if we have ANY significant digits at all (that is, if
  537. imax == 0).  If we don't, we suspect that adding FRACTBITS digits
  538. won't help, so we punt the whole thing.
  539. */
  540.             if (imax == 0)
  541.             {
  542.                 *fcnP = NULL;
  543.                 return;
  544.             }
  545.             cx *= FRACTFLOAT;
  546.             cy *= FRACTFLOAT;
  547.             *icxP = cx;
  548.             *icyP = cy;
  549.             *fcnP = FPXYboth;
  550.         }
  551.         else
  552.             *fcnP = IXYboth;
  553.     }
  554.     else
  555.         *fcnP = IXYboth;
  556. /*
  557. Now we check for special cases where one coefficient is zero (after
  558. integer conversion):
  559. */
  560.     if (*icxP == 0)
  561.         *fcnP = (*fcnP == FPXYboth) ? FPYonly : IYonly;
  562.     else if (*icyP == 0)
  563.         *fcnP = (*fcnP == FPXYboth) ? FPXonly : IXonly;
  564. }
  565.  
  566.  
  567. /*
  568.  * UnConvert() - Find User Coordinates From FractPoints
  569.  *
  570.  * The interesting thing with this routine is that we avoid calculating
  571.  * the matrix inverse of the device transformation until we really need
  572.  * it, which is to say, until this routine is called for the first time
  573.  * with a given coordinate space.
  574.  *
  575.  * We also only calculate it only once.  If the inverted matrix is valid,
  576.  * we don't calculate it; if not, we do.  We never expect matrices with
  577.  * zero determinants, so by convention, we mark the matrix is invalid by
  578.  * marking both X terms zero.
  579.  */
  580. void UnConvert(
  581.         struct XYspace *S,        /* relevant coordinate space */
  582.         struct fractpoint *pt,    /* device coordinates */
  583.         double *xp, double *yp)    /* where to store resulting x,y */
  584. {
  585.     double x, y;
  586.  
  587.     CoerceInverse(S);
  588.     x = pt->x;
  589.     y = pt->y;
  590.     *xp = S->tofract.inverse[0][0] * x + S->tofract.inverse[1][0] * y;
  591.     *yp = S->tofract.inverse[0][1] * x + S->tofract.inverse[1][1] * y;
  592. }
  593.  
  594.  
  595. /*
  596.  * Transformations
  597.  */
  598. /*
  599.  * Xform() - Transform Object in X and Y
  600.  *
  601.  * TYPE1IMAGER wants transformations of objects like paths to be identical
  602.  * to transformations of spaces.  For example, if you scale a line(1,1)
  603.  * by 10 it should yield the same result as generating the line(1,1) in
  604.  * a coordinate space that has been scaled by 10.
  605.  *
  606.  * We handle fonts by storing the accumulated transform, for example, SR
  607.  * (accumulating on the right).  Then when we map the font through space TD,
  608.  * for example, we multiply the accumulated font transform on the left by
  609.  * the space transform on the right, yielding SRTD in this case.  We will
  610.  * get the same result if we did S, then R, then T on the space and mapping
  611.  * an unmodified font through that space.
  612.  */
  613. struct xobject *t1_Xform(struct xobject *obj, double M[2][2])
  614. {
  615.     if (obj == NULL)
  616.         return (NULL);
  617.  
  618.     if (obj->type == FONTTYPE)
  619.     {
  620.         register struct font *F = (struct font *)obj;
  621.  
  622.         F = UniqueFont(F);
  623.         return ((struct xobject *)F);
  624.     }
  625.     if (obj->type == PICTURETYPE)
  626.     {
  627. /*
  628. In the case of a picture, we choose both to update the picture's
  629. transformation matrix and keep the handles up to date.
  630. */
  631.         register struct picture *P = (struct picture *)obj;
  632.         register struct segment *handles;    /* temporary path to transform handles */
  633.  
  634.         P = UniquePicture(P);
  635.         handles = PathSegment(LINETYPE, P->origin.x, P->origin.y);
  636.         handles = Join(handles,
  637.                PathSegment(LINETYPE, P->ending.x, P->ending.y));
  638.         handles = (struct segment *)Xform((struct xobject *)handles, M);
  639.         P->origin = handles->dest;
  640.         P->ending = handles->link->dest;
  641.         KillPath(handles);
  642.         return ((struct xobject *)P);
  643.     }
  644.  
  645.     if (ISPATHTYPE(obj->type))
  646.     {
  647.         struct XYspace pseudo;    /* local temporary space              */
  648.         PseudoSpace(&pseudo, M);
  649.         return ((struct xobject *)PathTransform((struct segment *)obj, &pseudo));
  650.     }
  651.  
  652.  
  653.     if (obj->type == SPACETYPE)
  654.     {
  655.         register struct XYspace *S = (struct XYspace *)obj;
  656.  
  657. /* replaced ISPERMANENT(S->flag) with S->references > 1 3-26-91 PNM */
  658.         if (S->references > 1)
  659.             S = CopySpace(S);
  660.         else
  661.             S->ID = NEXTID;
  662.  
  663.         MatrixMultiply(S->tofract.normal, M, S->tofract.normal);
  664.         /*
  665.                * mark inverted matrix invalid:
  666.                */
  667.         S->flag &= ~HASINVERSE(ON);
  668.  
  669.         FillOutFcns(S);
  670.         return ((struct xobject *)S);
  671.     }
  672.  
  673.     return (ArgErr("Untransformable object", obj, obj));
  674. }
  675.  
  676.  
  677. /*
  678.  * Transform() - Transform an Object
  679.  *
  680.  * This is the external user's entry point.
  681.  */
  682. struct xobject *t1_Transform(
  683.         struct xobject *obj,
  684.         double cxx, double cyx,
  685.         double cxy, double cyy) /* 2x2 transform matrix elements in row order */
  686. {
  687.     double M[2][2];
  688.  
  689. //    IfTrace1((MustTraceCalls), "Transform(%z,", obj);
  690. //    IfTrace4((MustTraceCalls), " %f %f %f %f)\n", &cxx, &cyx, &cxy, &cyy);
  691.  
  692.     M[0][0] = cxx;
  693.     M[0][1] = cyx;
  694.     M[1][0] = cxy;
  695.     M[1][1] = cyy;
  696.     ConsiderContext(obj, M);
  697.     return (Xform(obj, M));
  698. }
  699.  
  700.  
  701. /*
  702.  * Scale() - Special Case of Transform()
  703.  *
  704.  * This is a user operator.
  705.  */
  706. struct xobject *t1_Scale(struct xobject *obj, double sx, double sy)
  707. {
  708.     double M[2][2];
  709.  
  710. //    IfTrace3((MustTraceCalls), "Scale(%z, %f, %f)\n", obj, &sx, &sy);
  711.     M[0][0] = sx;
  712.     M[1][1] = sy;
  713.     M[1][0] = M[0][1] = 0.0;
  714.     ConsiderContext(obj, M);
  715.     return (Xform(obj, M));
  716. }
  717.  
  718.  
  719. /*
  720.  * Rotate() - Special Case of Transform()
  721.  *
  722.  * We special-case different settings of 'degrees' for performance
  723.  * and accuracy within the DegreeSin() and DegreeCos() routines themselves.
  724.  */
  725. struct xobject *xiRotate(
  726.         struct xobject *obj,    /* object to be transformed */
  727.         double degrees)            /* degrees of COUNTER-clockwise rotation */
  728. {
  729.     double M[2][2];
  730.  
  731. //    IfTrace2((MustTraceCalls), "Rotate(%z, %f)\n", obj, °rees);
  732.  
  733.     M[0][0] = M[1][1] = DegreeCos(degrees);
  734.     M[1][0] = -(M[0][1] = DegreeSin(degrees));
  735.     ConsiderContext(obj, M);
  736.     return (Xform(obj, M));
  737. }
  738.  
  739.  
  740. /*
  741.  * PseudoSpace() - Build a Coordinate Space from a Matrix
  742.  *
  743.  * Since we have built all this optimized code that, given an (x,y) and
  744.  * a coordinate space, yield transformed (x,y), it seems a shame not to
  745.  * use the same logic when we need to multiply an (x,y) by an arbitrary
  746.  * matrix that is not (initially) part of a coordinate space.  This
  747.  * subroutine takes the arbitrary matrix and builds a coordinate
  748.  * space, with all its nifty function pointers.
  749.  */
  750. void PseudoSpace(
  751.         struct XYspace *S,    /* coordinate space structure to fill out */
  752.         double M[2][2])        /* matrix that will become 'tofract.normal' */
  753. {
  754.     S->type = SPACETYPE;
  755.     S->flag = ISPERMANENT(ON) + ISIMMORTAL(ON);
  756.     S->references = 2;    /* 3-26-91 added PNM  */
  757.     S->tofract.normal[0][0] = M[0][0];
  758.     S->tofract.normal[1][0] = M[1][0];
  759.     S->tofract.normal[0][1] = M[0][1];
  760.     S->tofract.normal[1][1] = M[1][1];
  761.  
  762.     FillOutFcns(S);
  763. }
  764.  
  765.  
  766. /*
  767.  * Matrix Arithmetic
  768.  *
  769.  * Following the convention in Newman and Sproull, Interactive Computer Graphics,
  770.  * matrices are organized:
  771.  *        | cxx   cyx |
  772.  *        | cxy   cyy |
  773.  *
  774.  * A point is horizontal, for example:
  775.  *        [ x y ]
  776.  *
  777.  * This means that:
  778.  * x prime = cxx times x + cxy times y
  779.  * y prime = cyx times x + cyy times y
  780.  *
  781.  * I've seen the other convention, where transform matrices are
  782.  * transposed, equally often in the literature.
  783.  */
  784.  
  785.  
  786. /*
  787.  * MatrixMultiply() - Implements Multiplication of Two Matrices
  788.  *
  789.  * Implements matrix multiplication, A * B = C.
  790.  *
  791.  * To remind myself, matrix multiplication goes rows of A times columns
  792.  * of B.
  793.  * The output matrix may be the same as one of the input matrices.
  794.  */
  795. void MatrixMultiply(
  796.         double A[2][2],
  797.         double B[2][2],    /* input matrices */
  798.         double C[2][2])    /* output matrix */
  799. {
  800.     double txx, txy, tyx, tyy;
  801.  
  802.     txx = A[0][0] * B[0][0] + A[0][1] * B[1][0];
  803.     txy = A[1][0] * B[0][0] + A[1][1] * B[1][0];
  804.     tyx = A[0][0] * B[0][1] + A[0][1] * B[1][1];
  805.     tyy = A[1][0] * B[0][1] + A[1][1] * B[1][1];
  806.  
  807.     C[0][0] = txx;
  808.     C[1][0] = txy;
  809.     C[0][1] = tyx;
  810.     C[1][1] = tyy;
  811. }
  812.  
  813.  
  814. /*
  815.  * MatrixInvert() - Invert a Matrix
  816.  *
  817.  * My reference for matrix inversion was :hp1/Elementary Linear Algebra/
  818.  * by Paul C. Shields, Worth Publishers, Inc., 1968.
  819.  */
  820. void MatrixInvert(
  821.         double M[2][2],            /* input matrix */
  822.         double Mprime[2][2])    /* output inverted matrix */
  823. {
  824.     double D;    /* determinant of matrix M                      */
  825.     double txx, txy, tyx, tyy;
  826.  
  827.     txx = M[0][0];
  828.     txy = M[1][0];
  829.     tyx = M[0][1];
  830.     tyy = M[1][1];
  831.  
  832.     D = M[1][1] * M[0][0] - M[1][0] * M[0][1];
  833.     if (D == 0.0)
  834.         t1_abort("MatrixInvert:  can't");
  835.  
  836.     Mprime[0][0] = tyy / D;
  837.     Mprime[1][0] = -txy / D;
  838.     Mprime[0][1] = -tyx / D;
  839.     Mprime[1][1] = txx / D;
  840. }
  841.  
  842.  
  843. /*
  844.  * Initialization, Queries, and Debug
  845.  */
  846. /*
  847.  * InitSpaces() - Initialize Constant Spaces
  848.  *
  849.  * For compatibility, we initialize a coordinate space called USER which
  850.  * maps 72nds of an inch to pels on the default device.
  851.  */
  852. struct XYspace *USER = &identity;
  853.  
  854. void InitSpaces(void)
  855. {
  856.     extern char *DEFAULTDEVICE;
  857.  
  858.     IDENTITY->type = SPACETYPE;
  859.     FillOutFcns(IDENTITY);
  860.  
  861.     contexts[NULLCONTEXT].normal[1][0]
  862.         = contexts[NULLCONTEXT].normal[0][1]
  863.         = contexts[NULLCONTEXT].inverse[1][0]
  864.         = contexts[NULLCONTEXT].inverse[0][1] = 0.0;
  865.     contexts[NULLCONTEXT].normal[0][0]
  866.         = contexts[NULLCONTEXT].normal[1][1]
  867.         = contexts[NULLCONTEXT].inverse[0][0]
  868.         = contexts[NULLCONTEXT].inverse[1][1] = 1.0;
  869.  
  870.     USER->flag |= ISIMMORTAL(ON);
  871.     CoerceInverse(USER);
  872. }
  873.  
  874.  
  875. /*
  876.  * QuerySpace() - Returns the Transformation Matrix of a Space
  877.  *
  878.  * Since the tofract matrix of an XYspace includes the scale factor
  879.  * necessary to produce fractpel results (i.e., FRACTFLOAT), this
  880.  * must be taken out before we return the matrix to the user.  Fortunately,
  881.  * this is simple:  just multiply by the inverse of IDENTITY!
  882.  */
  883. void QuerySpace(struct XYspace *S, double *cxxP, double *cyxP, double *cxyP, double *cyyP)
  884. {
  885.     double M[2][2];        /* temp matrix to build user's answer           */
  886.  
  887.     if (S->type != SPACETYPE)
  888.     {
  889.         ArgErr("QuerySpace: not a space", S, NULL);
  890.         return;
  891.     }
  892.     MatrixMultiply(S->tofract.normal, IDENTITY->tofract.inverse, M);
  893.     *cxxP = M[0][0];
  894.     *cxyP = M[1][0];
  895.     *cyxP = M[0][1];
  896.     *cyyP = M[1][1];
  897. }
  898.  
  899.  
  900. /*
  901.  * FormatFP() - Format a Fixed Point Pel
  902.  *
  903.  * We format the pel as "dddd.XXXX", where XX's are hexidecimal digits,
  904.  * and the dd's are decimal digits.  This might be a little confusing
  905.  * mixing hexidecimal and decimal like that, but it is convenient
  906.  * to use for debug.
  907.  *
  908.  * We make sure we have N (FRACTBITS/4) digits past the decimal point.
  909.  */
  910. //#define  FRACTMASK   ((1<<FRACTBITS)-1)    /* mask for fractional part         */
  911.  
  912. //void FormatFP(
  913. //        char *string,    /* output string */
  914. //        fractpel fpel)    /* fractional pel input */
  915. //{
  916. //    char temp[8];
  917. //    char *s;
  918. //    char *sign;
  919. //
  920. //    if (fpel < 0)
  921. //    {
  922. //        sign = "-";
  923. //        fpel = -fpel;
  924. //    }
  925. //    else
  926. //        sign = "";
  927. //
  928. //    sprintf(temp, "000%x", fpel & FRACTMASK);
  929. //    s = temp + strlen(temp) - (FRACTBITS / 4);
  930. //
  931. //    sprintf(string, "%s%d.%sx", sign, fpel >> FRACTBITS, s);
  932. //}
  933.  
  934.  
  935. /*
  936.  * DumpSpace() - Display a Coordinate Space
  937.  */
  938. //void DumpSpace(struct XYspace *S)
  939. //{
  940. //    IfTrace4(TRUE, "--Coordinate space at %x,ID=%d,convert=%x,iconvert=%x\n",
  941. //         S, S->ID, S->convert, S->iconvert);
  942. //    IfTrace2(TRUE, "             |  %12.3f  %12.3f  |",
  943. //         &S->tofract.normal[0][0], &S->tofract.normal[0][1]);
  944. //    IfTrace2(TRUE, "   [  %p  %p ]\n", S->itofract[0][0], S->itofract[0][1]);
  945. //    IfTrace2(TRUE, "             |  %12.3f  %12.3f  |",
  946. //         &S->tofract.normal[1][0], &S->tofract.normal[1][1]);
  947. //    IfTrace2(TRUE, "   [  %p  %p ]\n", S->itofract[1][0], S->itofract[1][1]);
  948. //}
  949.